$(function() {
	getBdmById();
})

/**
 * 根据ID加载部门预算信息
 * @returns
 */
function getBdmById() {
    var bdmId = $("#bdmId").val();
    var param = {
        time: new Date(),
    }
    $.ajax({
        url: "/bdm/" + bdmId,
        data: param,
        dataType: 'json',
        method: 'get',
        success: function(result) {
            if ('SUCCESS' == result.result) {
            	var bdm = result.object;
            	$("#dept").val(bdm.dept);
                $("#title").val(bdm.title);dept
                $("#year").val(bdm.year);
                $("#budgetAll").val(bdm.budgetAll);
                $("#budgetCan").val(bdm.budgetCan);
                $("#budgetLock").val(bdm.budgetLock);
                $("#budgetHave").val(bdm.budgetHave);
                $("#signCode").val(bdm.signCode);
                $("#signTime").val(bdm.signTime);
                getBdmUserCondition(bdm.title, 1);
            }
        }
    });

}

/**
 * 使用情况
 * 
 * @returns
 */
function getBdmUserCondition(budget, type) {
	var param = {
		time : new Date(),
		type : type
	};
	$.ajax({
		url : '/eform/list/bdm/' + budget,
		method : 'get',
		data : param,
		dataType : 'json',
		success : function(result) {
			if ('SUCCESS' == (result.result)) {
				getBdmCost(budget, type);
				var obj = result.object;
				var data = obj.data;
				var total = obj.total;
				var html = "";
				for (var i = 0; i < data.length; i++) {
					var eform = data[i];
					html = html + "<tr>";
					html = html + "<td>" + eform.item_sqdh + "</td>";
					html = html + "<td>" + eform.item_ywmc + "</td>";
					html = html + "<td>" + eform.STATELABEL + "</td>";
					html = html + "<td>" + eform.cost + "</td>";
					html = html + "<td>已用量</td>";
					html = html + "</tr>";
				}
				$("#eforms-content").html(html);
			}
		}
	});
}

function getBdmCost(budget, type) {
	var param1 = {
		time : new Date(),
		type : type
	};
	var param2 = {
			time : new Date()
		};
	$.ajax({
		url : '/eform/cost/' + budget,
		method : 'get',
		data : param1,
		dataType : 'json',
		success : function(result) {
			if ('SUCCESS' == (result.result)) {
				var obj = result.object;
				$("#eform_cost").val(obj);
				$("#sum_cost").val(parseFloat($("#eform_cost").val()) + parseFloat($("#change_demand").val()));
			}
		}
	});
	$.ajax({
		url : '/bdm/dc/' + $("#bdmId").val(),
		method : 'get',
		data : param2,
		dataType : 'json',
		success : function(result) {
			if ('SUCCESS' == (result.result)) {
				var obj = result.message;
				$("#change_demand").val(obj);
				$("#sum_cost").val(parseFloat($("#eform_cost").val()) + parseFloat($("#change_demand").val()));
			}
		}
	});
}